<?php

namespace app\shop\model\product;
use app\common\model\product\RecommendList as RecommendListModel;

/**
 * 推荐列表模型
 */
class RecommendList extends RecommendListModel
{
    /**
     * 获取列表数据
     */
    public function getList($params)
    {
        $model = $this->alias('ls');
        if (isset($params['status']) && $params['status'] != '') {
            $model = $model->where('status', '=', $params['status']);
        }
        if (isset($params['module_id']) && $params['module_id'] != '') {
            $model = $model->where('module_id', '=', $params['module_id']);
        }
        // 查询列表数据
        return $model->join('product', 'product.product_id=ls.product_id', 'left')
            ->join('category', 'category.category_id=product.category_id', 'left')
            ->join('supplier', 'product.shop_supplier_id=supplier.shop_supplier_id', 'left')
            ->with(['product' => ['image.file' => function ($query) {
                $query->limit(1);
            }]])
            ->field('ls.*,category.category_id,category.name as category_name,supplier.shop_supplier_id,supplier.name as supplier_name,product.product_name')
            ->when(isset($params['shop_supplier_id']) && $params['shop_supplier_id'],function ($query) use($params) {
                $query->where('product.shop_supplier_id',$params['shop_supplier_id']);
            })
            ->when(isset($params['category_id']) && $params['category_id'],function ($query) use($params) {
                $query->where('product.category_id',$params['category_id']);
            })
            ->when(isset($params['search']) && $params['search'],function ($query) use($params) {
                $query->whereLike('product.product_name', $params['search'].'%');
            })
            ->order(['sort' => 'asc', 'create_time' => 'desc'])
            ->paginate($params);
    }

    /**
     * 获取列表数据
     */
    public function RecommendList($params)
    {
        $model = $this;
        // 查询列表数据
        $list = $model->where('module_id', '=', $params['module_id']) -> select();
        return $list;
    }


    /**
     * 详情
     */
    public static function getdetail($id){
        return (new static())->where('id', '=', $id)->find();
    }

    /**
     * 添加新记录
     */
    public function add($data)
    {
        $data['app_id'] = self::$app_id;
        return $this->save($data);
    }

    /**
     * 批量添加新记录
     */
    public function addAll($data)
    {
        foreach ($data as &$datum) {
            $datum['app_id'] = self::$app_id;
        }
        return $this->saveAll($data);
    }

    /**
     * 编辑记录
     */
    public function edit($data)
    {
        return $this->save($data);
    }

    /**
     * 删除记录
     */
    public function remove()
    {
        return $this->delete();
    }
}